//
//  WDGoutVehicleLocationViewController.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2025/2/19.
//

#import "WDGoutVehicleLocationViewController.h"
#import "WDGoutVehicleLocationViewModel.h"

@interface WDGoutVehicleLocationViewController () <MAMapViewDelegate>

@property (nonatomic, strong) WDGoutVehicleLocationViewModel *viewModel;

@property (nonatomic, strong) MAMapView *mapView;

@end

@implementation WDGoutVehicleLocationViewController

- (void)dealloc {
    
}

- (void)initWithVar {
    [super initWithVar];
        
}

- (void)initWithViewModel {
    _viewModel = [WDGoutVehicleLocationViewModel new];
    _viewModel.serviceModel = self.serviceModel;
    [_viewModel setupData];
}

- (void)initWithNavBar {
    [super initWithNavBar];
    
    [self initWithBackButton];
    
    self.navigationItem.title = self.viewModel.title;
}

- (void)initWithSubView {
    [super initWithSubView];
    
    [self initWithMapView];
}

- (void)initWithMapView {
    CGRect frame = CGRectMake(0, 0, kScreenW, kScreenH);
    _mapView = [[MAMapView alloc] initWithFrame:frame];
    _mapView.showsUserLocation = YES;
    _mapView.delegate = self;
    [_mapView setZoomLevel:17.5 animated:YES];
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    _mapView.customizeUserLocationAccuracyCircleRepresentation = YES;
    
    [self.view addSubview:_mapView];
    
    
    CLLocationCoordinate2D location = CLLocationCoordinate2DMake(self.viewModel.serviceModel.rescueLatitude, self.viewModel.serviceModel.rescueLongitude);
    float zoomLevel = 0.01;
    MACoordinateRegion region = MACoordinateRegionMake(location, MACoordinateSpanMake(zoomLevel, zoomLevel));
    [self.mapView setRegion:[self.mapView regionThatFits:region]];
    
    MAPointAnnotation *pointAnn = [[MAPointAnnotation alloc] init];
    pointAnn.coordinate = location;
    pointAnn.title = self.viewModel.serviceModel.plateNumber;
//    pointAnn.subtitle = self.viewModel.serviceModel.rescueAddress;
    [self.mapView addAnnotation:pointAnn];
    [self.mapView selectAnnotation:pointAnn animated:YES];
    
}

- (void)mapViewDidFailLoadingMap:(MAMapView *)mapView withError:(NSError *)error {
    debugLog(@"地图加载失败！");
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
    if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        MAPinAnnotationView *annotationView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.image = [UIImage imageNamed:@"ic_veh_map_loc"];
        annotationView.canShowCallout= YES;       //设置气泡可以弹出，默认为NO
//        annotationView.animatesDrop = YES;        //设置标注动画显示，默认为NO
        annotationView.draggable = NO;        //设置标注可以拖动，默认为NO
//        annotationView.pinColor = MAPinAnnotationColorRed;
        return annotationView;
    }
    return nil;
}

@end
